#include <iostream>
#include <unistd.h>
#include "instrumentor.h"

/*
{
	"otherData": {},
	"traceEvents": [{}, {
		"cat": "function",
		"dur": 53948,
		"name": "void a()",
		"ph": "X",
		"pid": 0,
		"tid": 3117617998,
		"ts": 5380573180541
	}, {
		"cat": "function",
		"dur": 52769,
		"name": "void b()",
		"ph": "X",
		"pid": 0,
		"tid": 3117617998,
		"ts": 5380573234663
	}, {
		"cat": "function",
		"dur": 53784,
		"name": "void c()",
		"ph": "X",
		"pid": 0,
		"tid": 3117617998,
		"ts": 5380573287585
	}, {
		"cat": "function",
		"dur": 160933,
		"name": "void f4()",
		"ph": "X",
		"pid": 0,
		"tid": 3117617998,
		"ts": 5380573180541
	}, {
		"cat": "function",
		"dur": 55172,
		"name": "void a()",
		"ph": "X",
		"pid": 0,
		"tid": 2508219374,
		"ts": 5380573389487
	}, {
		"cat": "function",
		"dur": 55204,
		"name": "void b()",
		"ph": "X",
		"pid": 0,
		"tid": 1727490996,
		"ts": 5380573389503
	}, {
		"cat": "function",
		"dur": 264388,
		"name": "void f2()",
		"ph": "X",
		"pid": 0,
		"tid": 1727490996,
		"ts": 5380573180497
	}, {
		"cat": "function",
		"dur": 264318,
		"name": "void f1()",
		"ph": "X",
		"pid": 0,
		"tid": 2508219374,
		"ts": 5380573180478
	}, {
		"cat": "function",
		"dur": 55194,
		"name": "void a()",
		"ph": "X",
		"pid": 0,
		"tid": 2858891031,
		"ts": 5380573389513
	}, {
		"cat": "function",
		"dur": 54603,
		"name": "void b()",
		"ph": "X",
		"pid": 0,
		"tid": 2858891031,
		"ts": 5380573445087
	}, {
		"cat": "function",
		"dur": 319274,
		"name": "void f3()",
		"ph": "X",
		"pid": 0,
		"tid": 2858891031,
		"ts": 5380573180519
	}]
}
*/
// #define CC_PROFILE 1

// #if CC_PROFILE
// #define HZ_FUNC_SIG() InstrumentationTimer timer(__FUNCSIG__);
// #else
// #define HZ_FUNC_SIG()
// #endif

void a()
{
    HZ_PROFILE_FUNCTION();
    usleep(50000);
}

void b()
{
    HZ_PROFILE_FUNCTION();
    usleep(50000);
}

void c()
{
    HZ_PROFILE_FUNCTION();
    usleep(50000);
}

void f1()
{
    HZ_PROFILE_FUNCTION();
    usleep(200000);
    a();
}

void f2()
{
    HZ_PROFILE_FUNCTION();
    usleep(200000);
    b();
}

void f3()
{
    HZ_PROFILE_FUNCTION();
    usleep(200000);
    a();
    b();
}

void f4()
{
    HZ_PROFILE_FUNCTION();
    a();
    b();
    c();
}

int main()
{
    //Instrumentor::Get().BeginSession("test-main");
    HZ_PROFILE_BEGIN_SESSION("Startup", "Profile.json");

    std::vector<std::thread> workers;

    workers.push_back(std::thread(f1));
    workers.push_back(std::thread(f2));
    workers.emplace_back(f3);
    workers.emplace_back(f4);

    for (auto &w : workers)
    {
        w.join();
    }

    //Instrumentor::Get().EndSession();
    HZ_PROFILE_END_SESSION();

    return 0;
}
